﻿using System;
using System.Collections.Generic;
 
using System.Text;

namespace GGSharp.Packets
{
    public class NewStatusPacket : HeaderPacket
    {
        protected string _description;
        protected int _show;
        protected int _time = 0;

        public NewStatusPacket()
        {
            this.Type = PacketType.GG_NEW_STATUS;
            this.Length = 4;
        }

        public NewStatusPacket(GGShowType show)
            : this(show, null) { }

        public NewStatusPacket(GGShowType show, string description)
            : this()
        {
            Description = description;
            Show = show;
        }

        public string Description
        {
            get { return _description; }
            set
            {
                if (value != null && value.Length > 70)
                    throw new Exception("Opis gadu moze miec maksymalnie 70 znakow");

                _description = value;
            }
        }

        public GGShowType Show
        {
            get { return (GGShowType)_show; }
            set { _show = (int)value; }
        }

        public override void Read(System.IO.BinaryReader reader)
        {
            //TODO:rozpoznawanie czasu by sie przydalo. obecnie pakuje wszystko do opisu co dziala bo przed czasem jest 0
            base.Read(reader);

            _show = reader.ReadInt32();

            int charsToRead = this.Length - 4; //dlugosc pakietu to show(4) + opis() + zero(1) + time(4)
            charsToRead = charsToRead > 0 ? charsToRead : 0;
            _description = Encoding.GetEncoding("windows-1250").GetString(reader.ReadBytes(charsToRead));

            //_time = reader.ReadInt32();
        }

        public override void Write(System.IO.BinaryWriter writer)
        {
            //ustawianie odpowiedniej dlugosci pakietu
            if (_description != null)
            {
                this.Length += _description.Length + 1; //ilosc znakow + zero + time
                if (_time != 0)
                    this.Length += 4;
            }

            base.Write(writer);

            writer.Write(_show);

            if (_description != null)
            {
                //writer.Write(_description);
                writer.Write(Encoding.GetEncoding("windows-1250").GetBytes(_description));
                writer.Write((byte)0);
                
                if (_time != 0)
                    writer.Write(_time);
            }
        }
    }
}
